using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityToolbarExtender;

namespace Miya.Hellow.Editor
{
    [InitializeOnLoad]
    public class ToolBarExtend
    {
        const string SCENE_NAME_WELCOME = "welcome";
        const string SCENE_NAME_INTRO = "sc_intro";
        private const string WAIT_FOR_INTRO = "EditorButtons_WaitForIntro";
        static ToolBarExtend()
        {
            ToolbarExtender.LeftToolbarGUI.Add(OnToolbarGUI);
            ToolbarExtender.RightToolbarGUI.Add(OnToolbarGUIRight);

            //UnityEditor.EditorApplication.playModeStateChanged += delegate (PlayModeStateChange _change)
            //{
            //    if (_change == PlayModeStateChange.EnteredPlayMode)
            //    {
            //        var val = PlayerPrefs.GetInt(WAIT_FOR_INTRO, 0);
            //        if (val == 1)
            //        {
            //            PlayerPrefs.SetInt(WAIT_FOR_INTRO, 0);
            //            SceneManager.LoadScene(SCENE_NAME_INTRO);
            //        }
            //    }
            //};
        }

        static void OnToolbarGUI()
        {
            if (Application.isPlaying)
                return;
            GUILayout.FlexibleSpace();

            if (GUILayout.Button(new GUIContent("Welcome", "游戏启动场景"), ToolbarStyles.mainButtonStyle))
            {
                SceneTool.OpenScene(SCENE_NAME_WELCOME);
            }
            if (GUILayout.Button(new GUIContent("启动", "游戏启动场景"), ToolbarStyles.mainButtonStyle))
            {
                SceneTool.OpenScene(SCENE_NAME_INTRO);
            }



        }

        static string PATH_PLUGINS=>Path.Combine(Application.dataPath, "Plugins");
        static void OnToolbarGUIRight()
        {
            //if (Application.isPlaying)
            //    return;
            GUILayout.FlexibleSpace();

            if (GUILayout.Button(new GUIContent("./", $"热资产数据主目录:\n {Application.streamingAssetsPath}"), ToolbarStyles.folderButtonStyle))
            {
                Application.OpenURL(Application.streamingAssetsPath);
            }
            if (GUILayout.Button(new GUIContent("~/", $"沙盒目录:\nApplication.persistentDataPath"), ToolbarStyles.folderButtonStyle))
            {
                Application.OpenURL($"{Application.persistentDataPath}");
            }

            if (GUILayout.Button(new GUIContent("Plugins", $"Plugins目录:\n{PATH_PLUGINS}"), ToolbarStyles.folderButtonStyle))
            {
                Application.OpenURL($"{PATH_PLUGINS}");
            }
            if (GUILayout.Button(new GUIContent("AA", $"Addressable 资产构建目录:\n{BundleBuilder.PATH_AA_BUILD_ROOT}"), ToolbarStyles.folderButtonStyle))
            {
                Application.OpenURL($"{BundleBuilder.PATH_AA_BUILD_ROOT}");
            }
            if (GUILayout.Button(new GUIContent("Pub", $"手机安装包构建目录:\n{Builder.PATH_BUILDING_OUT}"), ToolbarStyles.folderButtonStyle))
            {
                Application.OpenURL($"{Builder.PATH_BUILDING_OUT}");
            }
            if (GUILayout.Button(new GUIContent("DEL", $"删除本地存档记录"), ToolbarStyles.folderButtonStyle))
            {
                if (EditorUtility.DisplayDialog("确认", "确定要删除本地存档记录吗(regedit)", "确定", "取消"))
                    PlayerPrefs.DeleteAll(); PlayerPrefs.Save();
            }
            //if (GUILayout.Button(new GUIContent("远程数据地址", "数据主目录"), ToolbarStyles.folderButtonStyle))
            //{
            //    Application.OpenURL(APP.INFO_DOMAIN);
            //}



        }
    }
    static class ToolbarStyles
    {
        public static readonly GUIStyle mainButtonStyle;
        public static readonly GUIStyle folderButtonStyle;



        static ToolbarStyles()
        {
            mainButtonStyle = new GUIStyle("Command")
            {
                fontSize = 12,
                alignment = TextAnchor.MiddleCenter,
                imagePosition = ImagePosition.ImageAbove,
                fixedWidth = 60,
            };
            folderButtonStyle = new GUIStyle("Command")
            {
                fontSize = 12,
                alignment = TextAnchor.MiddleCenter,
                imagePosition = ImagePosition.ImageAbove,
                fixedWidth = 40,
            };
        }
    }
    public   static class SceneTool
    {
        static string sceneToOpen;

        public static void OpenScene(string sceneName)
        {
            string[] guids = AssetDatabase.FindAssets("t:scene " + sceneName, null);
            if (guids.Length == 0)
            {
                Debug.LogWarning("Couldn't find scene file");
            }
            else
            {
                string scenePath = AssetDatabase.GUIDToAssetPath(guids[0]);
                EditorSceneManager.OpenScene(scenePath);
            }
        }

        public static void StartScene(string sceneName)
        {
            if (EditorApplication.isPlaying)
            {
                EditorApplication.isPlaying = false;
            }

            sceneToOpen = sceneName;
            EditorApplication.update += OnUpdate;
        }

        static void OnUpdate()
        {
            if (sceneToOpen == null ||
                EditorApplication.isPlaying || EditorApplication.isPaused ||
                EditorApplication.isCompiling || EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }

            EditorApplication.update -= OnUpdate;

            if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
            {
                // need to get scene via search because the path to the scene
                // file contains the package version so it'll change over time
                string[] guids = AssetDatabase.FindAssets("t:scene " + sceneToOpen, null);
                if (guids.Length == 0)
                {
                    Debug.LogWarning("Couldn't find scene file");
                }
                else
                {
                    string scenePath = AssetDatabase.GUIDToAssetPath(guids[0]);
                    EditorSceneManager.OpenScene(scenePath);
                    EditorApplication.isPlaying = true;
                }
            }
            sceneToOpen = null;
        }
    }
    public static class Windows
    {
        [DllImport("user32.dll", SetLastError = true/*, ThrowonUnmappablechar = true, charset = charset.Auto*/)]
        public static extern int MessageBox(IntPtr handle, string message, string title, int type);//具体方法
    }
}


